<?php

namespace ztn\iot\sdk;

use ztn\iot\sdk\req\DeviceBatchStatusReq;
use ztn\iot\sdk\req\DeviceGetReq;
use ztn\iot\sdk\req\DeviceListReq;
use ztn\iot\sdk\req\DeviceSensorDataListReq;
use ztn\iot\sdk\req\DeviceSensorListWithReq;
use ztn\iot\sdk\req\DeviceSettingListReq;
use ztn\iot\sdk\req\DeviceSettingSendReq;
use ztn\iot\sdk\req\TokenGetReq;
use ztn\iot\sdk\util\HttpTool;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/9
 * Time: 16:04
 */

function setToken()
{

    $token = "${后台生成永久token}";
    HttpTool::$token = $token;
}

function refreshToken()
{

    $apiKey = "${apiKey}";
    $apiSecret = "${apiSecret}";

    $tokenGetReq = new TokenGetReq($apiKey, $apiSecret);

    IotTokenTool::refresh($tokenGetReq);
}

function listDevice()
{

    $req = new DeviceListReq();
    $req->setStatus("ONLINE");
    $list = IotDeviceTool::list($req);

    echo "------------------listDevice-------------------------";
    for ($i = 0, $j = sizeof($list); $i < $j; $i++) {
        $device = $list[$i];
        echo $device->deviceCore;
    }
    echo "-------------------------------------------";
    $deviceCoreList = [];
    for ($i = 0, $j = sizeof($list); $i < $j; $i++) {
        $device = $list[$i];
        if ($i == 0) {
            getDevice($device->deviceCore);
        } else if ($i < 5) {
            array_push($deviceCoreList, $device->deviceCore);
        }
    }
    batchDeviceStatus($deviceCoreList);
}

function batchDeviceStatus($deviceCoreList)
{

    $req = new DeviceBatchStatusReq();
    $req->setDeviceCoreList($deviceCoreList);
    $deviceStatusList = IotDeviceTool::batchDeviceStatus($req);
    echo "-------------------batchDeviceStatus------------------------";
    foreach ($deviceStatusList as $deviceStatus) {
        echo $deviceStatus->deviceCore . ' ' . $deviceStatus->status;
    }
    echo "-------------------------------------------";
}

function getDevice($deviceCore)
{

    $req = new DeviceGetReq($deviceCore);
    $device = IotDeviceTool::get($req);
    echo "--------------------getDevice-----------------------";
    echo $deviceCore;
    echo "-------------------------------------------";
    listSensor($deviceCore);
    listSetting($deviceCore);
    sendSetting($deviceCore);
}

function listSensor($deviceCore)
{

    $req = new DeviceSensorListWithReq();
    $req->setDeviceCore($deviceCore);
    $req->setWithExplain(true);
    $req->setWithParam(true);
    $req->setWithSetting(true);
    $deviceSensorWithList = IotDeviceSensorTool::list($req);
    echo "------------------listSensor------------------------";
    for ($i = 0, $j = sizeof($deviceSensorWithList); $i < $j; $i++) {
        $deviceSensorWith = $deviceSensorWithList[$i];
        echo $deviceSensorWith->port;
        foreach ($deviceSensorWith->explainList as $deviceExplain) {

        }
        forEach ($deviceSensorWith->settingList as $deviceSettingWithParamList) {

        }
        forEach ($deviceSensorWith->paramList as $deviceParam) {

        }
        if ($i == 0) {
            listSensorDataWithExplain($deviceCore, $deviceSensorWith->port);
        }
    }
    echo "------------------------------------------";
}

function listSensorDataWithExplain($deviceCore, $port)
{

    $endTime = time();
    $startTime = $endTime - (60 * 60 * 12);

    $req = new DeviceSensorDataListReq();
    $req->setDeviceCore($deviceCore);
    $req->setPort($port);
    $req->setEnableEval(true);
    $req->setStartTime($startTime);
    $req->setEndTime($endTime);

    $sensorDataWithExplain = IotDeviceSensorDataTool::listWithExplain($req);
    if (null != $sensorDataWithExplain) {
        forEach ($sensorDataWithExplain->explainList as $deviceExplain) {

        }
        forEach ($sensorDataWithExplain->dataList as $deviceSensorData) {

        }
    }

}

function listSetting($deviceCore)
{
    $req = new DeviceSettingListReq();
    $req->setDeviceCore($deviceCore);

    $list = IotDeviceSettingTool::list($req);
    echo "-----------------listSetting-------------------------";
    foreach ($list as $deviceSettingWithParamList) {
        echo $deviceSettingWithParamList->keyName;
    }
    echo "------------------------------------------";
}

function sendSetting($deviceCore)
{

    $req = new DeviceSettingSendReq();
    $req->setDeviceCore($deviceCore);
    $req->setPort("0");
    $req->setKeyName("getData");
    $req->setValue("null");

    $send = IotDeviceSettingTool::send($req);
    echo "------------------sendSetting------------------------";
    if (null != $send) {
        echo $send->error;
    }
    echo "------------------------------------------";
}

echo "start ...";
include './util/HttpTool.php';
include './req/TokenGetReqV1.php';
include './req/DeviceListReqV1.php';
include './req/DeviceBatchStatusReqV1.php';
include './req/DeviceGetReqV1.php';
include './req/DeviceSettingListReqV1.php';
include './req/DeviceSettingSendReqV1.php';
include './req/DeviceSensorDataListReqV1.php';
include './req/DeviceSensorListWithReqV1.php';
include './IotDeviceSensorTool.php';
include './IotDeviceSensorDataTool.php';
include './IotDeviceSettingTool.php';
include './IotTokenTool.php';
include './IotDeviceTool.php';
refreshToken();
listDevice();
?>